package cn.lby.tp.downloadtest.activity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.IOException;

import cn.lby.tp.downloadtest.BaseApplication;
import cn.lby.tp.downloadtest.R;
import download_temp.DownLoaderManger;
import download_temp.MultiDownloader;
import download_temp.view.NumberProgressBar;
import download_temp.view.OnProgressListener;

public class HttpConnectDownActivity extends BaseApplication implements OnProgressListener {

    private NumberProgressBar pb;//进度条
    private DownLoaderManger downLoader = null;

    private EditText fileName;
    private EditText download_url;
    private TextView threadNum, buffNum;
    private SeekBar threadCount, buffSize;
    private Button downThread;

    @Override
    protected int getLayout() {
        return R.layout.activity_httpconnect_down;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pb = findViewById(R.id.pb);
        downThread = findViewById(R.id.down_thread);
        fileName = findViewById(R.id.file_name);
        download_url = findViewById(R.id.download_url);
        threadCount = findViewById(R.id.threadCount);
        buffSize = findViewById(R.id.buffSize);
        threadNum = findViewById(R.id.threadNum);
        buffNum = findViewById(R.id.buffNum);


        // 线程数
        threadCount.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                threadNum.setText(String.valueOf(i));
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) { }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) { }
        });

        buffSize.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                buffNum.setText(String.valueOf(i));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });


        downThread.setOnClickListener(view -> {
            new Thread(() -> {
                int progress = threadCount.getProgress();
                int buffLen = buffSize.getProgress();
                String name = fileName.getText().toString();
                String url = download_url.getText().toString();

                // 多线程下载
                MultiDownloader down = new MultiDownloader(name, url);
                down.setThreadCount(progress);
                down.setBuffSize(buffLen * 1024);
                down.setListener(this);
                try {
                    down.download();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        });

    }

    @Override
    public void updateProgress(final int max, final int progress) {
        pb.setMax(max);
        pb.setProgress(progress);
    }
}
